import router from './router'
import store from './store'
// import { Message } from 'element-ui'
import NProgress from 'nprogress' // progress bar
import 'nprogress/nprogress.css' // progress bar style
import getPageTitle from './utils/get-page-title'
import { asyncRoutes } from '@/router/index.js'
// import { getToken } from '@/utils/auth' // get token from cookie
// import getPageTitle from '@/utils/get-page-title'

// NProgress.configure({ showSpinner: false }) // NProgress Configuration

// const whiteList = ['/login'] // no redirect whitelist

const list = ['/login', '/404']
// 导航守卫
router.beforeEach(async(to, from, next) => {
  // 启动页面跳转的进度条
  NProgress.start()
  // 获取token
  const token = store.state.user.token
  if (token) { // 如果有token
    if (to.path === '/login') {
      console.log('已经登录过了,不能取登录界面了')
      next('/') // 有token让他回到首页,不能去登录页
      NProgress.done()
    } else {
      //  页面跳转的时候判断是否有个人信息
      const userInfo = store.state.user.userInfo
      if (!userInfo.id) {
        const res = await store.dispatch('user/getUserProfile')
        console.log(asyncRoutes)
        const menus = res.data.roles.menus
        console.log('自带的', menus)
        const filterRouter = asyncRoutes.filter(item => {
          return menus.includes(item.children[0].name)
        })
        filterRouter.push({ path: '*', redirect: '/404', hidden: true })
        router.addRoutes(filterRouter) // 动态添加路由
        store.commit('menu/updateMenuList', filterRouter) // 调用方法保存组合后的路由
        next({
          ...to,
          replace: true
        })
      } else {
        next() // 如果去其他页面就放行
      }
    }
  } else {
    //  没有token
    // 是否去白名单的页面中
    if (list.includes(to.path)) {
      next() // 如果去login就放行
      NProgress.done()
    } else {
      next('/login') // 如果不是去login ,就跳回login,因为没有token
      NProgress.done()
    }
  }
})

router.beforeEach((to, from, next) => {
  document.title = getPageTitle(to.meta.title)
  next()
})
router.afterEach(() => {
  NProgress.done() // 让进度条结束
})
// router.beforeEach(async(to, from, next) => {
//   // start progress bar
//   NProgress.start()

//   // set page title
//   document.title = getPageTitle(to.meta.title)

//   // determine whether the user has logged in
//   const hasToken = getToken()

//   if (hasToken) {
//     if (to.path === '/login') {
//       // if is logged in, redirect to the home page
//       next({ path: '/' })
//       NProgress.done()
//     } else {
//       const hasGetUserInfo = store.getters.name
//       if (hasGetUserInfo) {
//         next()
//       } else {
//         try {
//           // get user info
//           await store.dispatch('user/getInfo')

//           next()
//         } catch (error) {
//           // remove token and go to login page to re-login
//           await store.dispatch('user/resetToken')
//           Message.error(error || 'Has Error')
//           next(`/login?redirect=${to.path}`)
//           NProgress.done()
//         }
//       }
//     }
//   } else {
//     /* has no token*/

//     if (whiteList.indexOf(to.path) !== -1) {
//       // in the free login whitelist, go directly
//       next()
//     } else {
//       // other pages that do not have permission to access are redirected to the login page.
//       next(`/login?redirect=${to.path}`)
//       NProgress.done()
//     }
//   }
// })

// router.afterEach(() => {
//   // finish progress bar
//   NProgress.done()
// })
